package main

import (
	"errors"
	"fmt"
	"github.com/globalsign/mgo"
	"github.com/globalsign/mgo/bson"
	"time"
)

type Person struct {
	Name  string
	Phone string
}

func main() {

	//session, err := mgo.Dial("mongodb://reet1:chatMMtahc@43.255.53.66:27072")
	//if err != nil {
	//	panic(err)
	//}

	host := "mongodb://reet1:chatMMtahc@43.255.53.66:27072"
	session, err := mgo.DialWithInfo(&mgo.DialInfo{
		Addrs:       []string{host},
		Timeout:     30,
		Username:    "",
		Password:    "",
		PoolLimit:   10,
		PoolTimeout: 30,
		MinPoolSize: 1,
	})
	defer session.Close()

	session.SetMode(mgo.Monotonic, true)
	session.SetPoolLimit(100)

	data, err := GetUnreadAssistantChatOne(session)
	fmt.Println("data:", data, err)

	// 删除群组历史消息记录
	// DeleteGroupHistory(session)

}

// 删除群组历史消息记录
func DeleteGroupHistory(session *mgo.Session) {
	c := session.DB("db_chat_history_new").C("tb_chat_group_message_new")
	if c == nil {
		return
	}
	selector := bson.M{"chattime": bson.M{"$lt": 1599737902}}
	info, err := c.RemoveAll(selector)
	if err != nil {
		fmt.Println("删除失败：", err)
	} else {
		fmt.Println("历史数据删除成功:", info.Removed)
	}

}

type UnreadChatGroupAssistantMessage struct {
	RecvUid     uint32 `bson: "RecvUid"`
	AssistantID uint32 `bson: "AssistantID"`
	//最后读取游标id
	LastReadMsgID string `bson: "LastReadMsgID"`
}

func GetUnreadAssistantChatOne(session *mgo.Session) (*UnreadChatGroupAssistantMessage, error) {
	c := session.DB("db_chat_unread_new").C("tb_chat_group_assistant_unread_new")
	if c == nil {
		return nil, errors.New("获取用户未读私聊消息集合错误")
	}
	data := new(UnreadChatGroupAssistantMessage)
	err := c.Find(bson.M{"recvuid": 3194000006, "assistantid": 20001}).One(&data)
	return data, err
}

// 新增数据
func InsertData(session *mgo.Session) error {
	c := session.DB("test").C("people")
	err := c.Insert(&Person{"Ale", "+555381169639"},
		&Person{"Cla", "+555384028510"})
	if err != nil {
		panic(err)
	}
	result := Person{}
	err = c.Find(bson.M{"name": "Ale"}).One(&result)
	if err != nil {
		panic(err)
	}
	fmt.Println("Phone:", result.Phone)
	c = session.DB("").C("")
	if c == nil {
		return errors.New("获取用户私聊消息集合错误")
	}
	data := Person{"lisi", "1314544545"}
	err = c.Insert(data)
	return err

}

// 查询数据
func SearchData(session *mgo.Session) ([]Person, error) {
	c := session.DB("").C("COLLECTION")
	if c == nil {
		return nil, errors.New("获取用户私聊消息集合错误")
	}
	dataArr := make([]Person, 0)

	selector := bson.M{"uid": 12344, "msgid": 12314, "status": 0}

	err := c.Find(selector).All(&dataArr)
	return dataArr, err
}

// 修改数据
func UpdateData(session *mgo.Session) error {
	c := session.DB("DATA_BASEA").C("COLLECTION")
	if c == nil {
		return errors.New("获取用户私聊消息集合错误")
	}
	selector := bson.M{"recvuid": Person{}, "senduid": Person{}}
	val := bson.M{"$set": bson.M{"lastreadmsgid": Person{}}}
	err := c.Update(selector, val)

	//如果是还没有这条记录
	if err != nil && err == mgo.ErrNotFound {
		err = c.Insert(Person{})
	}
	return err
}

// 删除数据
func DeleteData(session *mgo.Session) error {
	c := session.DB("DATA_BASEA").C("COLLECTION")
	if c == nil {
		return errors.New("获取用户私聊消息集合错误")
	}
	selector := bson.M{"Name": "张三"}
	_, err := c.RemoveAll(selector)
	err = c.Remove(selector)
	return err
}

// 查询数量
func SearchCount(session *mgo.Session) (int, error) {
	c := session.DB("DATA_BASEA").C("COLLECTION")
	if c == nil {
		return 0, errors.New("获取用户私聊消息集合错误")
	}
	t := time.Now()
	zero_tm := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()).Unix()
	twl_tm := time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location()).Unix() - 1
	groupid := 121
	uid := 1
	selector := bson.M{"groupid": groupid, "uid": uid, "chattype": 1}
	subAnd := bson.M{"$and": []bson.M{bson.M{"chattime": bson.M{"$gt": zero_tm}},
		bson.M{"chattime": bson.M{"$lt": twl_tm}},
		selector}}

	cnt, err := c.Find(subAnd).Count()
	return cnt, err
}
